﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.OleDb;
using System.Data;

namespace PulseApp
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page5 : Page
    {

        Person person;

        public Page5()
        {
            InitializeComponent();

            myDataSet = (pulseDataSet)(this.FindResource("pulseDataSet"));
            myAdapter = new pulseDataSetTableAdapters.personTableAdapter();
            myAdapter.Fill(myDataSet.person);
            System.Windows.Data.CollectionViewSource personViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("personViewSource")));
            personViewSource.View.MoveCurrentToFirst();
            
        }

        private void setPersonInfo(Person person)
        {

            this.lastNameBox.Text = person.getLastName();
            this.firstNameBox.Text = person.getFirstName();
            this.sexBox.Text = person.getSex();
            this.ageBox.Text = "" + person.getAge();
            this.heightBox.Text = "" + person.getHeight();
            this.weightBox.Text = "" + person.getWeight();
            this.hbpBox.Text = "" + person.getHbp();
            this.lbpBox.Text = "" + person.getLbp();
            this.streetBox.Text = person.getStreet();
            this.cityBox.Text = person.getCity();
            this.provinceBox.Text = person.getProvince();
            this.countryBox.Text = person.getCountry();
            this.postCodeBox.Text = person.getPostCode();
            this.phoneBox.Text = person.getPhone();
            this.faxBox.Text = person.getFax();
            this.emailBox.Text = person.getEmail();
        }

        private bool readPersonInfo()
        {
            bool sex = (this.sexBox.SelectedIndex == 0);
            int age = 0;
            int height = 0;
            int weight = 0;
            int hbp = 0;
            int lbp = 0;
            int id = 0;
            string message = "";
            bool error = false;
            int role;
            try
            {
                age = int.Parse(ageBox.Text.Trim());
            }
            catch (Exception ex)
            {
                message += "The age is not a number. Please input the correct age!\r\n";
                error = true;
            }
            try
            {
                if (this.metricButton.IsChecked == true)
                {
                    height = int.Parse(heightBox.Text.Trim());
                }
                else if (this.britishButton.IsChecked == true)
                {
                    height = (int)(int.Parse(ftBox.Text.Trim()) * 30.48 + int.Parse(inBox.Text.Trim()) * 2.54);
                }
                else
                {
                    message += "Please select unit.\r\t";
                    error = true;
                }
            }
            catch (Exception ex)
            {
                message += "The height is not a number. Please input the correct height!\r\n";
                error = true;
            }
            try
            {
                if (this.metricButton.IsChecked == true)
                {
                    weight = int.Parse(weightBox.Text.Trim());
                }
                else if (this.britishButton.IsChecked == true)
                {
                    weight = (int)(int.Parse(weightBox.Text.Trim())/2.205);
                }
                else
                {
                    message += "Please select unit.\r\t";
                    error = true;
                }
                
            }
            catch (Exception ex)
            {
                message += "The weight is not a number. Please input the correct weight!\r\n";
                error = true;
            }
            /*try
            {
                hbp = int.Parse(hbpBox.Text.Trim());
            }
            catch (Exception ex)
            {
                message += "The hbp is not a number. Please input the correct hbp!\r\n";
                error = true;
            }
            try
            {
                lbp = int.Parse(lbpBox.Text.Trim());
            }
            catch (Exception ex)
            {
                message += "The lbp is not a number. Please input the correct lbp!\r\n";
                error = true;
            */

            try
            {
                role = RoleBox.SelectedIndex;
            }
            catch (Exception ex)
            {
                role = 0;
            }
            
            if (!error)
            {
                person = new Person(id, lastNameBox.Text.Trim(), firstNameBox.Text.Trim(), sexBox.Text.Trim(), age, height, weight, hbp, lbp,
                    streetBox.Text.Trim(), cityBox.Text.Trim(), provinceBox.Text.Trim(), countryBox.Text.Trim(),
                    postCodeBox.Text.Trim(), phoneBox.Text.Trim(), faxBox.Text.Trim(), emailBox.Text.Trim(),UsernameBox.Text.Trim(),
                    PasswordBox.Text.Trim(), role);
                
            }
            else
            {
                MessageBox.Show(message);
            }
            return error;
        }

        private void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            
        }




        

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            if (!readPersonInfo())
            {
                
                
                person.insert(myAdapter);
                myAdapter.Update(myDataSet.person);
                person.setId((int)(myDataSet.person.Rows[myDataSet.person.Rows.Count - 1]["ID"]));
                CommonResources.Instance.setPerson(person);
                NavigationService.Navigate(new Uri("Index.xaml", UriKind.Relative));
            }
        }

        private void britishButton_Checked(object sender, RoutedEventArgs e)
        {
            this.weightLabel.Content = "Weight(lb): ";
            this.heightLabel.Content = "Height: ";
            if (!this.weightBox.Text.Trim().Equals(""))
            {
                this.weightBox.Text = (int)(int.Parse(weightBox.Text.Trim()) * 2.205) + "";
            }
            if (!this.heightBox.Text.Trim().Equals(""))
            {
                int heightm = int.Parse(heightBox.Text.Trim());
                int fts = (int)(heightm / 30.48);
                int ins = (int)((heightm-fts*30.48)/2.54);
                this.ftBox.Text = fts + "";
                this.inBox.Text = ins + "";
            }
            this.heightBox.Visibility = System.Windows.Visibility.Hidden;
            this.ftBox.Visibility = System.Windows.Visibility.Visible;
            this.inBox.Visibility = System.Windows.Visibility.Visible;
            this.ftTextBlock.Visibility = System.Windows.Visibility.Visible;
            this.inTextBlock.Visibility = System.Windows.Visibility.Visible;
        }

        private void metricButton_Checked(object sender, RoutedEventArgs e)
        {
            this.weightLabel.Content = "Weight(KG): ";
            this.heightLabel.Content = "Height(CM): ";
            if (!this.weightBox.Text.Trim().Equals(""))
            {
                this.weightBox.Text = (int)(int.Parse(weightBox.Text.Trim()) / 2.205) +"";
            }
            if (!this.ftBox.Text.Trim().Equals(""))
            {
                this.heightBox.Text = (int)(int.Parse(ftBox.Text.Trim()) * 30.48 + int.Parse(inBox.Text.Trim()) * 2.54) + "";
            }
            this.heightBox.Visibility = System.Windows.Visibility.Visible;
            this.ftBox.Visibility = System.Windows.Visibility.Hidden;
            this.inBox.Visibility = System.Windows.Visibility.Hidden;
            if(this.ftTextBlock!=null)this.ftTextBlock.Visibility = System.Windows.Visibility.Hidden;
            if(this.inTextBlock!=null)this.inTextBlock.Visibility = System.Windows.Visibility.Hidden;
        }



        
    }
}
